﻿using System;
using System.Collections.Generic;
using System.Data;

namespace GUI.BR
{
    using EXT;
    using BLL;
    using DevExpress.Utils;
    public partial class frmChitietHanghoa : GUI.Danhmuc.ChitietDanhmuc.frmInputChitietDanhmuc
    {
        public frmChitietHanghoa()
        {
            InitializeComponent();
        }

        public static DataRow DR;

        private void Load_Danhsach()
        {
            HanghoaBLL Hanghoa_BLL = new HanghoaBLL();
            DataTable dt = Hanghoa_BLL.Hanghoa_Select_All_Vali();
            gcDanhsach.DataSource = dt;
            if (dt.Rows.Count > 0)
            {
                palDSHanghoa.Text = String.Format("Danh sách hàng hóa___Có: {0} mặt hàng", dt.Rows.Count);
            }
            else palDSHanghoa.Text = "Danh sách hàng hóa___Chưa có mặt hàng nào";
        }

        private void frmChitietHanghoa_Load(object sender, EventArgs e)
        {
            WaitDialogForm wdf = new WaitDialogForm("Đang load dữ liệu...", "Xin chờ trong giây lát.");
            Load_Danhsach();
            wdf.Close();
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            frmHanghoa frmHH = new frmHanghoa(true);
            frmHH.MyEvent_LoadData_DanhsachHH += new MyEvent_LoadData(frmHH_MyEvent_LoadData_DanhsachHH);
            frmHH.ShowDialog();
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            DR = gvDanhsach.GetFocusedDataRow();
            frmHanghoa frmHH = new frmHanghoa(false);
            frmHH.MyEvent_LoadData_DanhsachHH += new MyEvent_LoadData(frmHH_MyEvent_LoadData_DanhsachHH);
            frmHH.ShowDialog();
        }

        void frmHH_MyEvent_LoadData_DanhsachHH(object sender, ELoadData e)
        {
            Load_Danhsach();
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            Guid Id = gvDanhsach.GetFocusedRowCellValue("Id").GetGuid();
            if (MsgC.MsgQuestionYN("Bạn có chắc muốn xóa hàng hóa này không?")==System.Windows.Forms.DialogResult.Yes)
            {
                HanghoaBLL Hanghoa_BLL = new HanghoaBLL();
                GiahanghoaBLL Giahanghoa_BLL = new GiahanghoaBLL();
                Hanghoa_BLL._sqlMapper.BeginTransaction();
                Giahanghoa_BLL.Giahanghoa_Delete(Id);
                if (Hanghoa_BLL.Hanghoa_Delete(Id))
                {
                    Hanghoa_BLL._sqlMapper.CommitTransaction();
                    Load_Danhsach();
                    MsgC.MsgOK("Đã xóa hàng hóa thành công");
                }
                else
                {
                    Hanghoa_BLL._sqlMapper.RollBackTransaction();
                    Hanghoa_BLL._sqlMapper.BeginTransaction();
                    if (Hanghoa_BLL.Hanghoa_Delete0_Update(Id))
                    {
                        Hanghoa_BLL._sqlMapper.CommitTransaction();
                        Load_Danhsach();
                        MsgC.MsgOK("Đã ẩn hàng hóa thành công");
                    }
                    else
                    {
                        MsgC.MsgWarning("Không thể kết nối đến CSDL");
                        Hanghoa_BLL._sqlMapper.RollBackTransaction();
                    }
                }
            }
        }

        private void btnDanhsachhanghoaKodungnua_Click(object sender, EventArgs e)
        {
            frmMathangngungkinhdoanh frmngung = new frmMathangngungkinhdoanh();
            frmngung.MyEvent_LoadData_Tailai += new MyEvent_LoadData(frmngung_MyEvent_LoadData_Tailai);
            frmngung.ShowDialog();
        }

        void frmngung_MyEvent_LoadData_Tailai(object sender, ELoadData e)
        {
            Load_Danhsach();
        }

        private void btnLichsugiaban_Click(object sender, EventArgs e)
        {
            BR.frmLichsugiaban frmlsgb = new frmLichsugiaban(gvDanhsach.GetFocusedRowCellValue("Id").GetGuid());
            frmlsgb.ShowDialog();
        }

        private void txtTailai_Click(object sender, EventArgs e)
        {
            Load_Danhsach();
        }
    }
}
